package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long uid) {
        return userInfoMapper.selectById(uid);
    }

    @Override
    public PageResult queryUserList(Integer page, Integer pagesize, String city, String nickname, Long id) {
//        先构建条件
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(city)){
            queryWrapper.lambda().like(UserInfo::getCity,city);
        }
        if(StringUtils.isNotBlank(nickname)){
            queryWrapper.lambda().like(UserInfo::getNickname,nickname);
        }
        if(id!=null){
            queryWrapper.lambda().eq(UserInfo::getId,id);
        }

//        构建分页的条件
        Page<UserInfo> page1 = new Page<>(page, pagesize);
        IPage<UserInfo> iPage = userInfoMapper.selectPage(page1, queryWrapper);
        return new PageResult(page,pagesize,(int)iPage.getTotal(),iPage.getRecords());
    }
}
